package pt.ua.helpdesk.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class FaqAdd extends Composite {

	public FaqAdd() {

		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel_4 = new VerticalPanel();
		verticalPanel.add(verticalPanel_4);

		final VerticalPanel verticalPanel_3 = new VerticalPanel();
		verticalPanel_4.add(verticalPanel_3);

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel_3.add(horizontalPanel_2);
		horizontalPanel_2.setWidth("100%");
		horizontalPanel_2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel_2.setSpacing(5);

		final Label dadosDoFaqLabel = new Label("Dados do FAQ");
		horizontalPanel_2.add(dadosDoFaqLabel);
		dadosDoFaqLabel.setStyleName("lblSectionTitle");

		final VerticalPanel verticalPanel_2 = new VerticalPanel();
		verticalPanel_3.add(verticalPanel_2);

		final Label label = new Label("Questão:");
		verticalPanel_2.add(label);
		label.setStyleName("label");

		final TextBox txtQuestao = new TextBox();
		verticalPanel_2.add(txtQuestao);
		txtQuestao.setWidth("450");
		txtQuestao.setStyleName("textbox");

		final Label respostaLabel = new Label("Resposta:");
		verticalPanel_2.add(respostaLabel);
		respostaLabel.setStyleName("label");

		final TextArea txtResposta = new TextArea();
		verticalPanel_2.add(txtResposta);
		txtResposta.setVisibleLines(20);
		txtResposta.setWidth("450");
		txtResposta.setStyleName("textarea");

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel_3.add(horizontalPanel);
		horizontalPanel.setSpacing(5);
		horizontalPanel.setWidth("100%");
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final Label lblAlerta = new Label("");
		
		final Button btnAdicionar = new Button();
		horizontalPanel.add(btnAdicionar);
		btnAdicionar.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				
				if(txtQuestao.getText().equals("") || txtResposta.getText().equals(""))
					lblAlerta.setText("Tem de preencher os Dados!");
				else
				{
					Integer idPerson = new Integer(Integer.parseInt(Default.getSession("IDPerson").toString()));
					
					lblAlerta.setText("a processar dados...");
					ServiceFaq.Util.getInstance().addFaq(idPerson, txtQuestao.getText(), txtResposta.getText(), new AdicionarAsyncCallback(lblAlerta));
				}
				
			}
		});
		btnAdicionar.setStyleName("button");
		btnAdicionar.setText("Adicionar");

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel_3.add(horizontalPanel_1);

		horizontalPanel_1.add(lblAlerta);
		lblAlerta.setStyleName("erro");
		verticalPanel.setStyleName("loginPanel");
		verticalPanel.setWidth("100%");


	}
	
	/**
	 * An asynchronous callback is the primary interface that a caller
	 * must implement to receive a response from a remote procedure call
	 * 
	 * This class is the implementation of AsyncCallback for the register
	 * user service
	 * 
	 * @author Amigos do Noddy
	 */	
	  static class AdicionarAsyncCallback implements AsyncCallback {
		  Label lbl;
		  
		  /**
		   * Class constructor
		   * @param html Widget that can contain arbitrary HTML
		   */
		  public AdicionarAsyncCallback(Label lbl) {
			  this.lbl= lbl;
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {
			  Boolean ret = (Boolean)object;
			  String message = "";
			  
			  if (ret.booleanValue())
				  message = "FAQ adicionado com Sucesso!";
			  else
				  message = "Ocorreu um Erro inesperado!";
				  
    		  DOM.setInnerHTML(lbl.getElement(), message);
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
	}

}
